#region File Header
//----------------------------------------------------------------
// Copyright (C) 2002 Stefan Kyntchev
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------
#endregion File Header

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Security;
using AirWeb.Security;
using AirWeb.Support.Diagnostics;

namespace FlashBack.Web
{
	/// <summary>
	/// Summary description for ManageRoles.
	/// </summary>
	public partial class ManageRoles : PageBase
	{


		public ManageRoles()
		{
			Page.Init += new System.EventHandler(Page_Init);
		}

		protected void Page_Load(object sender, System.EventArgs e)
		{
			Response.Expires = -1;
			ValidateCurrentUserIsAdministrator();
			BuildRoleTable();
			ErrorText.Visible = false;
		}

		protected void BuildRoleTable()
		{
			DataTable dt = new DataTable();
			DataRow dr;
			dt.Columns.Add(new DataColumn("Name", typeof(string)));
			dt.Columns.Add(new DataColumn("Note", typeof(string)));

			foreach (Role a in SecurityMgr.Instance.Roles)
			{
				if(a == null)
					continue;
				dr = dt.NewRow();
				dr[0] = a.Name;
				dr[1] = a.Description;

				dt.Rows.Add(dr);
			}
            
			RolesGrid.DataSource = new DataView(dt);
			RolesGrid.DataBind();
		}

		public void ItemCommand(object sender, DataGridCommandEventArgs e)
		{
			if(e.CommandName == "DeleteRole")
			{
				DeleteRole(e.Item.Cells[0].Text);
			}
			if(e.CommandName == "ManageAccess")
			{
				Session["Role to Edit"] = e.Item.Cells[0].Text;
				Server.Transfer("ManageAccess.aspx");
			}
		}

		protected void DeleteRole(string i_role)
		{
			try
			{
				SecurityMgr.Instance.RemoveRole(i_role);
				SecurityMgr.Instance.Serialize();
				ApplicationLog.WriteTrace("Role deleted. Role:" + i_role);				
			}
			catch(ArgumentException ex)
			{
				DisplayError(ex);
				ApplicationLog.WriteError("Error occured while deleting a role: " + ex.Message);
			}

			BuildRoleTable();
		}

		protected void DisplayError(Exception ex)
		{
			ErrorText.Text = "Error executing request. " + ex.Message;				
			ErrorText.Visible = true;
		}

		protected void AddRole(object sender, CommandEventArgs e)
		{
			try
			{
				SecurityMgr.Instance.AddRole(newRoleName.Text,newRoleDescription.Text);
				SecurityMgr.Instance.Serialize();
				ApplicationLog.WriteTrace("Role added. Role:" + newRoleName.Text);				
			}
			catch(ArgumentException ex)
			{
				DisplayError(ex);
				ApplicationLog.WriteError("Error occured while adding a role: " + ex.Message);
			}

			BuildRoleTable();
		}

		protected void onRowBound(object sender, DataGridItemEventArgs e)
		{
			// Don't allow editing or deleting of the built in Administrator role or the role that the active user 
			if(e.Item.Cells[0].Text == "Administrator" || e.Item.Cells[0].Text == CurrentUser.UserRole.Name)
				e.Item.Cells[2].Visible = false;
		}

		protected void Page_Init(object sender, EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
		}

		#region Web Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion
	}
}
